package com.shangsheng.controller;

import org.apache.commons.dbcp.BasicDataSource;

import javax.sql.DataSource;
import java.sql.*;

public class JdbcDatasources {
   private static String sql="SELECT * from USER ";
   private  static String url="jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC\n&useSSL=true";
    private String user="root";
    private String password="";

    private static DataSource createDataSource() {
        BasicDataSource result = new BasicDataSource();
       result.setDriverClassName(com.mysql.jdbc.Driver.class.getName());
       result.setUrl(String.format(url, "user"));
        result.setUsername("root");
       result.setPassword("");
        return result;
    }


    //String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?";

    public void dataSources(){
        try{

            Class.forName("com.mysql.cj.jdbc.Driver");
            //1.
            // Connection conn = DriverManager.getConnection(url, user, password);
            //  PreparedStatement preparedStatement = conn.prepareStatement(sql);
            //2.
             Connection connection = JdbcDatasources.createDataSource().getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                System.out.println( rs.getString("name"));
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        JdbcDatasources datasources =new JdbcDatasources();
        datasources.dataSources();
    }
}
